clear;
close all;
cd C:\_bcoe\_Irene_Data\;

c=clock;
day=[num2str(c(1)) '_' num2str(c(2)) '_' num2str(c(3)) ];
if day(7)=='_'
    day=[day(1:5), '0',day(6:end)];
end
if length(day)==9;
    day=[day(1:8), '0',day(9:end)];
end

eval(['mkdir ' day]);
eval(['cd ' day]);
clc;
figure(1);set(gcf,'MenuBar', 'none')
uimenufcn(gcf,'WindowCommandWindow');% to prevent from text input messing up the code
file_name=input('Input file name:','s');
if length(file_name)==0;disp('exited program');return;end
if exist([file_name '.mat'],'file')
    disp(['the file: ''' file_name '.mat'' already exists. please start again with a different file name. ' ])
    return
else
    A=[];
    A.name=[file_name '.mat'];
    A.clock=round(clock);
    save(file_name,'A');
    %bcoe_eyelink2_setup(file_name)
end



c=define_colors;
c.BACKGROUND=c.D_GREY;
c.TEXT01=c.WHITE;
A.colors=c;



[w,screenRect] = SCREEN(0,'OpenWindow',0,[],16);	
pause(.1);
SCREEN(w,'fillrect',c.BACKGROUND); 
[buffer,rect]  = SCREEN(0,'OpenOffscreenWindow',0,[],16);
SCREEN(buffer,'fillrect',c.BACKGROUND); 
c_x=screenRect(3)/2;
c_y=screenRect(4)/2;
setmouse(c_x*2,c_y*2);
figure(1);set(gcf,'position',[1 31 c_x*2 (c_y*2)-70])
pause(0)
% for now....
dist2screen=60;
screenwidth=36;
if dist2screen*screenwidth
	[ang ecc]=cart2polar(dist2screen,screenwidth/2);
else
	ang=30;
end	
pixperdeg=screenRect(3)/(ang*2);


text_size=46; % actually in pixels, i think
line_spacing=1.5; % 
textrow=text_size*line_spacing;
% put into saved struct

clear text_01
text_01=[...
'One of the most important exports of Australia is    ';...
'wine. Grapes were originally planted in the Hunter   ';...
'Valley on the east coast, but now there are wine     ';...
'growing areas in many other parts of the country.    ';...
'The warm climate and the rainfall near the coast     ';...
'make the land suitable for the industry. Winemakers  ';...
'from Australia have won many prizes in competitions. '];

clear vext_01
vext_01=[...
'One of the jilk important vezpept of Australia is    ';...
'wome. Grapes were originally bafjult in the Hunter   ';... 
'Jerzam on the zeat coast, but now there are wine     ';...
'flobnis areas in many glepo parts of the berkald.    ';... 
'The warm  yurpab and the torbbret near the coast     ';...
'make the land mirabol  for the industry. Thoncrogur  ';...
'from Australia have pum many  fifoc in competitions. '];


%must have at least one space after EACH line for this to work
v1_text1=[0; mod(find(text_01'==' '),length(text_01))]; % add zeros to start to help deliminate
v2_text1=find(v1_text1==0);

t_top =(1+((1:size(text_01,1))-1)+3) *textrow ;
t_bottom=((1:size(text_01,1))+4) *textrow ;
top=[];
bottom=[];
lr=(v1_text1)*(text_size/2);
left=lr(1:end-1)+.75*text_size;
right=lr(2:end);
right(right==0)=size(text_01,2)*(text_size/2);
right=right+.75*text_size;
for i =length(v2_text1):-1:2
    top(v2_text1(i-1):v2_text1(i))=t_top(i-1);
    bottom(v2_text1(i-1):v2_text1(i))=t_bottom(i-1);
end
top(1)=[];
bottom(1)=[];
A.text_01_rects=round([left top' right bottom']);


clear text_02
text_02=[...
'One of the most important attractions in Hong Kong   ';...
'is shopping. The citizens love to shop in the local  ';...
'markets and department stores, but so do the         ';...
'millions of tourists who come to visit each year.    ';... 
'The high quality goods and low prices make the city  ';...
'ideal for shoppers. Visitors also discover that Hong ';...
'Kong restaurants are among the finest in the world.  '];  
clear vext_02
vext_02=[...
'One of the jilk important  thoncrogur in Hong Kong   ';...
'is flobnis.  The citizens wome to shop in the local  ';...
'vezpept and department stores, but so do the         ';...
'torbbret of tourists who come to fifoc each zeat.    ';... 
'The high mirabol goods and low yurpab make the city  ';...
'glepo for  bafjult. Visitors also berkald  that Hong ';...
'Kong restaurants pum among the jerzam in the world.  ']; 


v1_text2=[0; mod(find(text_02'==' '),length(text_02))]; % add zeros to start to help deliminate
v2_text2=find(v1_text2==0);

t_top =(1+((1:size(text_02,1))-1)+3) *textrow ;
t_bottom=((1:size(text_02,1))+4) *textrow ;
top=[];
bottom=[];
lr=(v1_text2)*(text_size/2)
left=lr(1:end-1)+.75*text_size;
right=lr(2:end);
right(right==0)=size(text_02,2)*(text_size/2);
right=right+.75*text_size
for i =length(v2_text2):-1:2
    top(v2_text2(i-1):v2_text2(i))=t_top(i-1);
    bottom(v2_text2(i-1):v2_text2(i))=t_bottom(i-1);
end
top(1)=[];
bottom(1)=[];
text_02_rects=[left top' right bottom'];

clear text_03
text_03=[...
'Many  people have pets at  home.          ';...
'The most salidap  pets are cats and dogs. ']; 


v1_text3=[0; mod(find(text_03'==' '),length(text_03))]; % add zeros to start to help deliminate
v2_text3=find(v1_text3==0);

t_top =(1+((1:size(text_03,1))-1)+5) *textrow ;
t_bottom=((1:size(text_03,1))+6) *textrow ;
top=[];
bottom=[];
lr=(v1_text3)*(text_size/2)
left=lr(1:end-1)+.75*text_size;
right=lr(2:end);
right(right==0)=size(text_03,2)*(text_size/2);
right=right+.75*text_size
for i =length(v2_text3):-1:2
    top(v2_text3(i-1):v2_text3(i))=t_top(i-1);
    bottom(v2_text3(i-1):v2_text3(i))=t_bottom(i-1);
end
top(1)=[];
bottom(1)=[];
A.text_03_rects=[left top' right bottom'];








SCREEN(buffer,'TextStyle',0);% 0=plain, 1=bold, 2=italics, 3=1&2,  4=underlined, 5=4&1,6=4&2, 7=4&3. rem(?,8)
[oldFontName,oldFontNumber]=SCREEN(buffer,'TextFont','MS Mincho'); % nice
[oldFontName,oldFontNumber]=SCREEN(buffer,'TextFont','GungsuhChe'); %  serifs
[oldFontName,oldFontNumber]=SCREEN(buffer,'TextFont','MS Gothic'); % ok
[oldFontName,oldFontNumber]=SCREEN(buffer,'TextFont','Lucida Console');% wide
[oldFontName,oldFontNumber]=SCREEN(buffer,'TextFont','Courier New'); % wide
[oldFontName,oldFontNumber]=SCREEN(buffer,'TextFont','GulimChe'); % nicer

SCREEN(buffer,'TextSize',text_size);
which_text=0;
    buttons=0; % prevent from skipping mouse button loop
    while buttons(1)==0  
        [x y buttons]=getmouse;
        warning off MATLAB:DeprecatedLogicalAPI
        [keyIsDown,secs,keyCode] = KbCheck; % Warning: mxSetLogical is deprecated and will not be supported in the future. Please refer to the Release Notes for information on updating your code
        if find(keyCode)
            find(keyCode);
            switch sum(find(keyCode))
                case 27 % ESC on pcwin
                    SCREEN(w,'Close');
                    SCREEN(buffer,'Close');
                    close(1)
                    buttons=1;
                case 32% = spacebar
                    screen(w,'waitblanking');
                    SCREEN(w,'fillrect',c.BACKGROUND); 
                    which_text=0;
                case 96% = keypad.
                    
                    for i=1:78
                        SCREEN(w,'FrameRect',c.RED,text_01_rects(i,:))
                    end
                    
                case 95% = keypad0
                    screen(w,'waitblanking');
                    %SCREEN(w,'fillrect',c.BACKGROUND); 
                    
                    top   =round((floor(y/textrow)) *textrow );
                    bottom=round((ceil (y/textrow)) *textrow );
                    %ttext=current_text(row);
                    %twidth=SCREEN(w,'TextWidth',ttext);
                    switch which_text
                        case 1
                            v1_text=v1_text1;
                            v2_text=v2_text1;
                        case 2
                            v1_text=v1_text2;
                            v2_text=v2_text2;
                        case 3
                            v1_text=v1_text3;
                            v2_text=v2_text3;
                        otherwise
                            v2_text=0;
                    end % switch
                            
                    if (floor(y/textrow)-4)<length(v2_text)-1 & (floor(y/textrow)-3)>0 & which_text>0
                        %row=floor(y/textrow)
                        row_data=round([[0;v1_text(v2_text(floor(y/textrow)-3):v2_text(ceil(y/textrow)-3)-1)+1.5] * (text_size/2);c_x*2]);
                        right=min(row_data(row_data>x));
                        left=max(row_data(row_data<=x));
                    else
                        left=1;
                        right=(c_x*2);
                    end                    
                    SCREEN(w,'FrameRect',c.RED,[left top right bottom ])
                    
                    
                case 97% = keypad1
                    SCREEN(buffer,'fillrect',c.BACKGROUND); 
                    for i=1:size(text_01,1)
                        SCREEN(buffer,'DrawText',text_01(i,:),text_size,textrow*(i+3)+text_size,c.TEXT01);
                    end
                    screen(w,'waitblanking');
                    SCREEN('CopyWindow',buffer,w);   
                    which_text=1;
                case 98% = keypad2
                    SCREEN(buffer,'fillrect',c.BACKGROUND); 
                    for i=1:size(text_02,1)
                        SCREEN(buffer,'DrawText',text_02(i,:),text_size,textrow*(i+3)+text_size,c.TEXT01);
                    end
                    screen(w,'waitblanking');
                    SCREEN('CopyWindow',buffer,w);                    
                    which_text=2;
                case 99% = keypad3
                    SCREEN(buffer,'fillrect',c.BACKGROUND); 
                    for i=1:size(text_03,1)
                        SCREEN(buffer,'DrawText',text_03(i,:),text_size,(textrow*(i+5))+text_size,c.TEXT01);
                    end
                    screen(w,'waitblanking');
                    SCREEN('CopyWindow',buffer,w);                    
                    which_text=3;
                case 100% = keypa4
                    SCREEN(buffer,'fillrect',c.BACKGROUND); 
                    for i=1:size(vext_01,1)
                        SCREEN(buffer,'DrawText',vext_01(i,:),text_size,textrow*(i+3)+text_size,c.TEXT01);
                    end
                    screen(w,'waitblanking');
                    SCREEN('CopyWindow',buffer,w);                    
                    which_text=1;
                case 101% = keypad5
                    SCREEN(buffer,'fillrect',c.BACKGROUND); 
                    for i=1:size(vext_02,1)
                        SCREEN(buffer,'DrawText',vext_02(i,:),text_size,textrow*(i+3)+text_size,c.TEXT01);
                    end
                    screen(w,'waitblanking');
                    SCREEN('CopyWindow',buffer,w);                    
                    which_text=2;
%                 case 102% = keypad6
%                     SCREEN(buffer,'fillrect',c.BACKGROUND); 
%                     for i=1:size(vext_03,1)
%                         SCREEN(buffer,'DrawText',vext_03(i,:),text_size,(textrow*i)+text_size,c.TEXT01);
%                     end
%                     screen(w,'waitblanking');
%                     SCREEN('CopyWindow',buffer,w);                    
%                     which_text=3;
            end 
        end
        warning on MATLAB:DeprecatedLogicalAPI
        
    end
    
    
  close(1)
    SCREEN(w,'Close');
    SCREEN(buffer,'Close');
    return

